package com.brother.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.brother.database.DataBase;
import com.brother.model.Signal;
import com.brother.realtime.DataReceiverServer;
import com.brother.rest.RESTController;
import com.brother.service.RealDataService;
import com.brother.util.BackupUtils;
import com.brother.util.CommonProperties;
import com.brother.util.RecoverRealtimeDataFromReport;

/**
 * Servlet implementation class DataReceiverServlet
 */
public class DataReceiverServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger log = Logger.getLogger(DataReceiverServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DataReceiverServlet() {
        super();
    }
private DataReceiverServer dataServer ;
	/**
	 * @see Servlet#init(ServletConfig)
	 */
private boolean stopped = false;
	public void init(){
		final String tcpPort = this.getInitParameter("port");
		this.dataServer  = new  DataReceiverServer();
		RealDataService service = new RealDataService();
		service.updateAllLastDataSignal(Signal.na);
		Thread dataCleaner = new Thread(){
			int count = 0;
			public void run(){
				while(!stopped){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						log.error("", e);
					}
					
					if(count % CommonProperties.getTruncateInterval() ==  0){
						RealDataService service = new RealDataService();
						service.TruncateRealtimeData();
					}
					// 到了恢复realtimedata时间，为了防止意外产生，而导致消失实时数据
					if((count+1) % (CommonProperties.getTruncateInterval() + 300) ==  0){
						new Thread(){
							public void run(){
								try {
									RecoverRealtimeDataFromReport.recover();
								} catch (SQLException e) {
									log.error("recover error=", e);
								}
								try{
									BackupUtils.runBackup();
								}catch(Exception exx){
									log.error("backup error=", exx);
								}
							}
						}.start();
					}
					count +=1;
				}
			}
		};
		dataCleaner.setPriority(Thread.MIN_PRIORITY);
		dataCleaner.start();
		dataServer.startListener(tcpPort);
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		this.dataServer.destroy();
		stopped = true;
		RESTController.stopStatusUpdater();
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);
	}

}
